package com.mazihao.emos.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mazihao.emos.common.util.AjaxResult;
import com.mazihao.emos.config.shiro.JwtUtil;
import com.mazihao.emos.controller.form.InsertMeetingForm;
import com.mazihao.emos.controller.form.SearchMeetingByPageForm;
import com.mazihao.emos.db.pojo.TbMeeting;
import com.mazihao.emos.exception.EmosException;
import com.mazihao.emos.service.MeetingService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author mazihao
 */
@RestController
@RequestMapping("/meeting")
public class MeetingController {

    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private MeetingService meetingService;

    @PostMapping("/searchMeetingByPage")
    public AjaxResult searchMeetingByPage(@RequestBody @Valid SearchMeetingByPageForm form, @RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);
        int page = form.getPage();
        int length = form.getLength();
        PageHelper.startPage(page, length);
        ArrayList<HashMap<String, Object>> meetingList = meetingService.searchMeetingByPage(userId);
        PageInfo<HashMap<String, Object>> pageInfo = new PageInfo<>(meetingList);
        return AjaxResult.ok().put("result", pageInfo);
    }

//    @PostMapping("/updateMeetingInfo")
//    @ApiOperation("更新会议")
//    @RequiresPermissions(value = {"ROOT", "MEETING:UPDATE"}, logical = Logical.OR)
//    public R updateMeetingInfo(@Valid @RequestBody UpdateMeetingInfoForm form){
//        if(form.getType()==2&&(form.getPlace()==null||form.getPlace().length()==0)){
//            throw new EmosException("线下会议地点不能为空");
//        }
//        DateTime d1= DateUtil.parse(form.getDate()+" "+form.getStart()+":00");
//        DateTime d2= DateUtil.parse(form.getDate()+" "+form.getEnd()+":00");
//        if(d2.isBeforeOrEquals(d1)){
//            throw new EmosException("结束时间必须大于开始时间");
//        }
//        if(!JSONUtil.isJsonArray(form.getMembers())){
//            throw new EmosException("members不是JSON数组");
//        }
//        HashMap param=new HashMap();
//        param.put("title", form.getTitle());
//        param.put("date", form.getDate());
//        param.put("place", form.getPlace());
//        param.put("start", form.getStart() + ":00");
//        param.put("end", form.getEnd() + ":00");
//        param.put("type", form.getType());
//        param.put("members", form.getMembers());
//        param.put("desc", form.getDesc());
//        param.put("id", form.getId());
//        param.put("instanceId", form.getInstanceId());
//        param.put("status", 1);
//        meetingService.updateMeetingInfo(param);
//        return R.ok().put("result","success");
//    }

    @PostMapping("/insertMeeting")
    @ApiOperation("添加会议")
    @RequiresPermissions(value = {"ROOT", "MEETING:INSERT"}, logical = Logical.OR)
    public AjaxResult insertMeeting(@RequestBody @Valid InsertMeetingForm form, @RequestHeader("token") String token) {
        if (form.getType() == 2 && (form.getPlace().isEmpty())) {
            throw new EmosException("线下会议地点不能为空");
        }
        DateTime d1 = DateUtil.parse(form.getDate() + " " + form.getStart());
        DateTime d2 = DateUtil.parse(form.getDate() + " " + form.getEnd());
        if (d2.isBeforeOrEquals(d1)) {
            throw new EmosException("结束时间不能小于开始时间");
        }
        if (!JSONUtil.isJsonArray(form.getMembers())) {
            throw new EmosException("members不是JSON数组");
        }
        TbMeeting meeting = new TbMeeting();
        meeting.setUuid(UUID.randomUUID().toString(true));
        meeting.setTitle(form.getTitle());
        meeting.setCreatorId((long)jwtUtil.getUserId(token));
        meeting.setDate(form.getDate());
        meeting.setStart(form.getStart() + ":00");
        meeting.setPlace(form.getPlace());
        meeting.setEnd(form.getEnd() + ":00");
        meeting.setType((short)form.getType());
        meeting.setMembers(form.getMembers());
        meeting.setDesc(form.getDesc());
        meeting.setStatus((short)1);
        meetingService.insertMeeting(meeting);
        return AjaxResult.ok().put("result", "success");
    }
}
